package com.zrgk.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.zrgk.bean.DeptBean;
import com.zrgk.bean.EmpBean;
import com.zrgk.bean.RoleBean;
import com.zrgk.service.DeptService;
import com.zrgk.service.EmpService;
import com.zrgk.service.RoleService;
import com.zrgk.util.PageUtil;

@Controller
@RequestMapping(value="emp.do")
public class EmpController {
	@Autowired
	@Qualifier(value="empService")
	private EmpService empService;
	@Autowired
	@Qualifier(value="deptService")
	private DeptService deptService;
	@Autowired
	@Qualifier(value="roleService")
	private RoleService roleService;
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params="method=findEmpByPage")
	public String findEmpByPage(HttpServletRequest request){
		String nowPage = request.getParameter("page");
		
		PageUtil pageUtil=new PageUtil();
		if(nowPage!=null&&nowPage!=""){
			pageUtil.setNowPage(Integer.parseInt(nowPage));	
		}
		
		Map<String, String> map=new HashMap<String, String>();
		String empName = request.getParameter("empName");
		if(empName!=null&&empName!=""){
			map.put("empName", empName);
		}
		
		String empPhone = request.getParameter("empPhone");
		if(empPhone!=null&&empPhone!=""){
			map.put("empPhone", empPhone);
		}
		
		String empDeptId=request.getParameter("empDeptId");
		if(empDeptId!=null&&"0".equals(empDeptId)){
			map.put("empDeptId", empDeptId);
		}
		pageUtil.setSearchInfo(map);
		
		empService.findEmpByPage(pageUtil);
		request.setAttribute("pageUtil", pageUtil);
		return "/html/resource/sys/employee/list.jsp";
	}
	
	/**
	 * 
	 * @param request
	 * @param empId
	 * @return
	 */
	@RequestMapping(params="method=findEmpById")
	public String findEmpById(HttpServletRequest request,String empId){
		EmpBean emp = empService.findEmpById(Integer.parseInt(empId));
		request.setAttribute("emp", emp);
		return "/html/resource/sys/employee/view.jsp";
	}
	
	/**
	 * 
	 * @param request
	 * @param empId
	 * @return
	 */
	@RequestMapping(params="method=updateEmpBefore")
	public String updateEmpBefore(HttpServletRequest request,String empId){
		EmpBean emp = empService.findEmpById(Integer.parseInt(empId));
		request.setAttribute("emp", emp);
		List<DeptBean> depts = deptService.findAllDept();
		request.setAttribute("depts", depts);
		return "/html/resource/sys/employee/edit.jsp";
	}
	
	@RequestMapping(params="method=updateEmp")
	public String updateEmp(HttpServletRequest request,EmpBean emp){
		empService.updateEmp(emp);
		return "/html/resource/sys/employee/result.jsp";
	}
	
	@RequestMapping(params="method=addEmpBefore")
	public String addEmpbefore(HttpServletRequest request){
		List<DeptBean> depts = deptService.findAllDept();
		request.setAttribute("depts",depts);
		return "/html/resource/sys/employee/add.jsp";
	}
	
	@RequestMapping(params="method=checkEmpNumber")
	public void checkEmpNumber(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			String number = request.getParameter("number");
			boolean result = empService.checkEmpNumber(number);
			if(result){
				out.print("true");
			}else{
				out.print("false");
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params="method=updateEmpState")
	public void updateEmpState(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			String state = request.getParameter("state");
			String id = request.getParameter("id");
			EmpBean emp=new EmpBean();
			emp.setId(Integer.parseInt(id));
			if(state.equals("1")){
				emp.setState(2);
			}else{
				emp.setState(1);
			}
			empService.updateEmpState(emp);
			out.print("true");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params="method=deleteEmp")
	public void deleteEmp(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			String id = request.getParameter("id");
			empService.deleteEmp(Integer.parseInt(id));
			out.print("true");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params="method=addEmp")
	public String addEmp(HttpServletRequest request,EmpBean emp){
		Date date=new Date();
		emp.setCreateDate(date);
		emp.setLastDate(date);
		emp.setUsername(emp.getNumber());
		empService.addEmp(emp);
		request.setAttribute("emp", emp);
		return "/html/resource/sys/employee/result.jsp";
	}
	
	@RequestMapping(params="method=authoriseEmpBefore")
	public String authoriseEmpBefore(HttpServletRequest request){
		List<RoleBean> roles = roleService.findAllRole();
		String id = request.getParameter("id");
		EmpBean emp = empService.findEmpById(Integer.parseInt(id));
		request.setAttribute("roles",roles);
		request.setAttribute("emp", emp);
		return "/html/resource/sys/employee/emp_authorise.jsp";
	}
	
	@RequestMapping(params="method=authoriseEmp")
	public String authoriseEmp(HttpServletRequest request){
		String id = request.getParameter("empId");
		String roleId = request.getParameter("roleId");
		EmpBean emp=new EmpBean();
		emp.setId(Integer.parseInt(id));
		RoleBean role=new RoleBean();
		role.setId(Integer.parseInt(roleId));
		emp.setRole(role);
		empService.authoriseEmp(emp);
		emp=empService.findEmpById(Integer.parseInt(id));
		request.setAttribute("emp", emp);
		return "/html/resource/sys/employee/result.jsp";
	}
	
	
	public EmpService getEmpService() {
		return empService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	
}
